package net.bwie.flink;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

/**
 * 创建DataStream案例
 * @author xuanyu
 * @date 2025/10/27
 */
public class _01CreateDataStreamDemo {

	public static void main(String[] args) throws Exception{
		// 1. 执行环境-env
		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
		env.setParallelism(1);

		// 2.数据源-source
		/*
			为了测试方便，可以从本地集合创建DataStream数据流
		 */
		// todo 将列表List中数据转换为数据流DataStream
		ArrayList<String> list = new ArrayList<>();
		list.add("hello");
		list.add("spark");
		list.add("flink");
		list.add("sql");
		DataStreamSource<String> stream1 = env.fromCollection(list);
		stream1.print("collection");

		// todo 可以认为将数组中数据转换为数据流DataStream
		DataStreamSource<String> stream2 = env.fromElements("hello", "spark", "flink", "sql");
		stream2.print("elements");

		// 5.触发执行-execute
		env.execute("CreateDataStreamDemo") ;
	}

}
